// pages/home/home.js

Page({

  /**
   * 页面的初始数据
   */
  data: {
    //轮播图配置
    autoplay: true,
    interval: 3000,
    duration: 1200,
    articalList: [],
   
  },
// 跳转到搜索页面
search: function () {
  wx.navigateTo({
    url: '/pages/search/search'
  })
},
toText:function(){
  wx.navigateTo({
    url: '/pages/text/text',
  })
},

// 
  /**
   * 生命周期函数--监听页面加载
   */

  onLoad: function (options) {
   //url=http://82.157.94.115:2005/
    var that = this; 
    wx.request({
      url: 'http://82.157.94.115:2005/content',
      method:'GET',
      header: {
        'content-type': 'application/json'
      },
      data:{
      },
      success:(res)=>{
        console.log("成功")
        console.log(res.data)
        console.log(res.data.obj1)
        for (let a of res.data.obj1) {
          a.timestr = ((new Date(a.content_time)).toLocaleString()).slice(0,11)
        }
        that.setData({
          articalList:res.data.obj1,      
        })
      },
      fail:(err)=>{
        console.log(err)
      }
    })
    var data = {
      "datas": [
        {
          "id": 1,
          "imgurl": "http://82.157.94.115:2009/static/images/ho_banner1.png"
        },
        {
          "id": 2,
          "imgurl": "http://82.157.94.115:2009/static/images/ho_banner2.png"
        },
        {
          "id": 3,
          "imgurl": "http://82.157.94.115:2009/static/images/ho_banner3.jpg"
        },
        {
          "id": 4,
          "imgurl": "http://82.157.94.115:2009/static/images/ho_banner4.png"
        }
      ]
    };
    that.setData({
      lunboData: data.datas
    })
  },
  
zan: function (e) {
  var that = this;
  
  var index = e.currentTarget.dataset.index;
  var message = this.data.articalList;
  
  for (let i in message) { //遍历列表数据
    if (i == index) { //根据下标找到目标
      var collectStatus = false
      if (message[i].collected == 0) { //如果是没点赞+1
        collectStatus = true
        message[i].collected = parseInt(message[i].collected) + 1
      } else {
        collectStatus = false
        message[i].collected = parseInt(message[i].collected) - 1
      }
      wx.showToast({
        title: collectStatus ? '收藏成功' : '取消收藏',
      })
    }
    
  }
  this.setData({
    articalList: message
  })
  
  


},
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.bindAnimation();
    
  },
 
  bindAnimation(){
    var this_ = this;
      var animation = wx.createAnimation({
        duration: 6000,
        timingFunction: 'linear',
        transformOrigin:"100% 0 0"
      })
      animation.translateX('-100%').step();
      this.setData({
        animation:animation.export(),
      })
      //设置循环动画
      this.animation = animation;
      setInterval(function(){
        //第二个动画 文字位置初始化
        this.Animation2();
 
        //延迟播放滚动动画（效果会更好点）
        setTimeout(function(){
          this.animation.translateX('-100%').step();
          this.setData({
            animation: animation.export(),
          })
        }.bind(this),200);
 
        
      }.bind(this),5000);
 
  },
 
  /**
   * 第二个动画 文字位置初始化
   */
  Animation2(){
    var this_ = this;
    var animation2 = wx.createAnimation({
      duration: 0,
      timingFunction: 'linear',
      transformOrigin:"100% 0 0"
    })
    animation2.translateX('100%').step();
    this_.setData({
      animation:animation2.export(),
    })
  },
 
  /**
   * 解决小程序退出、隐藏后台动画停止
   */
  onHide: function () {
    //解决小程序退出、隐藏后台动画停止
    //重新触发动画方法即可
    this.bindAnimation();
},
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },



  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})